package com.yanftch.jsonformat.json2ets.parser.typescript

import com.google.gson.JsonArray
import com.google.gson.JsonNull
import com.google.gson.JsonObject
import com.google.gson.JsonPrimitive
import com.intellij.ui.components.Panel
import kotlin.reflect.jvm.internal.impl.load.kotlin.JvmType

open class TsPrimitiveConverter : JsonTraver {
    override fun traversePrimitive(asJsonPrimitive: JsonPrimitive, key: String?): String {
        return when {
            asJsonPrimitive.isString -> {
                "string"
            }

            asJsonPrimitive.isNumber -> {
                "number"
            }

            asJsonPrimitive.isBoolean -> {
                "boolean"
            }

            else -> "object"
        }
    }

    override fun traverseNull(jsonNull: JsonNull, key: String?): String {
        return "object"
    }

    override fun traverseArray(jsonArray: JsonArray, key: String?): String {
        var ret = ""
        if (jsonArray.size() > 0) {
            val jsonItemValue = jsonArray[0]
            if (jsonItemValue.isJsonObject && !jsonItemValue.isJsonArray) {
                val objectType = traverseSingleObject(jsonItemValue.asJsonObject, key)
                ret = "$objectType[]"
            } else if (jsonItemValue.isJsonPrimitive) {
                val type = traversePrimitive(jsonItemValue.asJsonPrimitive, null)
                ret = "$type[]"
            }
            return ret
        }
        return "object[]"
    }

    override fun traverseSingleObject(jsonObject: JsonObject, key: String?): String {
        return "object"
    }

    protected fun toCamelcase(vararg names: String): String {
        return names.reduce { acc, string ->
            (uppercaseFirstChar(acc) + uppercaseFirstChar(string)).replace(" ", "")
        }
    }

    private fun uppercaseFirstChar(key: String): String = key[0].toUpperCase() + key.substring(1, key.length)


    val TYPE_OBJECT = "Object"

    protected fun getDefaultValueByType(type: String?): Pair<String, Any> {
        /**
         * string
         * number
         * boolean
         * BBBBData
         * BBBBDataItems[]
         */
        if (type == null) return Pair("obj", TYPE_OBJECT)
        return when (type) {
            "string" -> Pair(type, "\"\"")
            "number" -> Pair(type, 0)
            "boolean" -> Pair(type, false)
             else -> {
                 if (type.contains("[]")) {
                     return Pair("[]", "[]")
                 } else {
                     return Pair("obj", "new $type()")
                 }
             }
        }
    }
}
